from sqlalchemy import func

from exts import db, get_time
from flask_login import UserMixin


# orm模型
# todo User
class User(db.Model, UserMixin):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(45), nullable=False, unique=True)
    password = db.Column(db.String(45), nullable=False)
    power = db.Column(db.Integer, nullable=False)

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)


# todo Root
class Root(db.Model):
    __tablename__ = "root"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(45), nullable=False)

    # 外键
    id_user = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref=db.backref("root", uselist=False))


# todo Teacher
class Teacher(db.Model):
    __tablename__ = "teacher"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(45), nullable=False)
    phone = db.Column(db.String(45), nullable=False)

    # 外键
    id_user = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref=db.backref("teacher", uselist=False))


# todo Student
class Student(db.Model):
    __tablename__ = "student"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(45), nullable=False)
    phone = db.Column(db.String(45), nullable=False)
    parent_phone = db.Column(db.String(45), nullable=False)
    age = db.Column(db.Float, nullable=False)
    material_cost = db.Column(db.Float, nullable=False)

    # 外键
    id_user = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref=db.backref("student", uselist=False))


# todo Classes
class Classes(db.Model):
    __tablename__ = "classes"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(45), nullable=False)
    room = db.Column(db.String(45), nullable=False)
    time = db.Column(db.String(45), nullable=False)
    department = db.Column(db.String(45), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_teacher = db.Column(db.Integer, db.ForeignKey("teacher.id"))
    teacher = db.relationship("Teacher", backref="classes")


# todo StudentClassesRel
class StudentClassesRel(db.Model):
    __tablename__ = "student_classes_rel"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_student = db.Column(db.Integer, db.ForeignKey("student.id"))
    student = db.relationship("Student", backref="student_classes_rel")
    id_classes = db.Column(db.Integer, db.ForeignKey("classes.id"))
    classes = db.relationship("Classes", backref="student_classes_rel")


# todo TestPaper
class TestPaper(db.Model):
    __tablename__ = "test_paper"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    name = db.Column(db.String(200), nullable=False)
    state = db.Column(db.Integer, nullable=False)  # 0:制作, 1:发布, 2:截止

    # 外键
    id_teacher = db.Column(db.Integer, db.ForeignKey("teacher.id"))
    teacher = db.relationship("Teacher", backref="test_paper")
    id_classes = db.Column(db.Integer, db.ForeignKey("classes.id"))
    classes = db.relationship("Classes", backref="test_paper")

    create_time = db.Column(db.DateTime, server_default=func.now())
    update_time = db.Column(db.DateTime, server_default=func.now(), onupdate=func.now())
    is_delete = db.Column(db.Integer, server_default='0')


# todo BaseQuestion
class BaseQuestion(db.Model):
    __tablename__ = "base_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question_type = db.Column(db.Integer, nullable=False)  # 0:单选题,1:判断题,2:简答题,3:多选题,4:图片题
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_test_paper = db.Column(db.Integer, db.ForeignKey("test_paper.id"))
    test_paper = db.relationship("TestPaper", backref="base_question")


# todo SingleChoiceQuestion
class SingleChoiceQuestion(db.Model):
    __tablename__ = "single_choice_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(200), nullable=False)
    a = db.Column(db.String(200), nullable=False)
    b = db.Column(db.String(200), nullable=False)
    c = db.Column(db.String(200), nullable=False)
    d = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(2), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref=db.backref("single_choice_question", uselist=False))


# todo JudgmentalQuestion
class JudgmentalQuestion(db.Model):
    __tablename__ = "judgmental_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref=db.backref("judgmental_question", uselist=False))


# todo MultipleChoiceQuestion
class MultipleChoiceQuestion(db.Model):
    __tablename__ = "multiple_choice_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(200), nullable=False)
    a = db.Column(db.String(200), nullable=False)
    b = db.Column(db.String(200), nullable=False)
    c = db.Column(db.String(200), nullable=False)
    d = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(45), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref=db.backref("multiple_choice_question", uselist=False))


# todo ShortAnswerQuestion
class ShortAnswerQuestion(db.Model):
    __tablename__ = "short_answer_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(500), nullable=False)
    answer = db.Column(db.String(500), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref=db.backref("short_answer_question", uselist=False))


# todo PictureQuestion
class PictureQuestion(db.Model):
    __tablename__ = "picture_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    question = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(200), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref=db.backref("picture_question", uselist=False))


# 学生答卷，记录一个学生的作答情况与成绩
# 成绩的实现方式：学生主动交卷会计算成绩
# todo TestPaperAnswer
class TestPaperAnswer(db.Model):
    __tablename__ = "test_paper_answer"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    score = db.Column(db.String(45), nullable=False)
    status = db.Column(db.Integer, nullable=False)

    # 外键
    id_test_paper = db.Column(db.Integer, db.ForeignKey("test_paper.id"))
    test_paper = db.relationship("TestPaper", backref="test_paper_answer")
    id_student = db.Column(db.Integer, db.ForeignKey("student.id"))
    student = db.relationship("Student", backref="test_paper_answer")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo QuestionAnswer
class QuestionAnswer(db.Model):
    __tablename__ = "question_answer"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    my_answer = db.Column(db.String(200), nullable=False)
    true_or_false = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_test_paper_answer = db.Column(db.Integer, db.ForeignKey("test_paper_answer.id"))
    test_paper_answer = db.relationship("TestPaperAnswer", backref="question_answer")
    id_base_question = db.Column(db.Integer, db.ForeignKey("base_question.id"))
    base_question = db.relationship("BaseQuestion", backref="question_answer")


# todo File
class File(db.Model):
    __tablename__ = "file"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    filename = db.Column(db.String(45), nullable=False)
    path = db.Column(db.String(200), nullable=False)
    size = db.Column(db.String(45), nullable=False)

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo QuestionBank
class QuestionBank(db.Model):
    __tablename__ = "question_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    name = db.Column(db.String(200), nullable=False)

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo SingleChoiceQuestionBank
class SingleChoiceQuestionBank(db.Model):
    __tablename__ = "single_choice_question_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    question = db.Column(db.String(200), nullable=False)
    a = db.Column(db.String(200), nullable=False)
    b = db.Column(db.String(200), nullable=False)
    c = db.Column(db.String(200), nullable=False)
    d = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(2), nullable=False)
    knowledge_points = db.Column(db.String(200))
    analysis = db.Column(db.String(500))

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="single_choice_question_bank")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo MultipleChoiceQuestionsBank
class MultipleChoiceQuestionsBank(db.Model):
    __tablename__ = "multiple_choice_questions_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    question = db.Column(db.String(200), nullable=False)
    a = db.Column(db.String(200), nullable=False)
    b = db.Column(db.String(200), nullable=False)
    c = db.Column(db.String(200), nullable=False)
    d = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(10), nullable=False)
    knowledge_points = db.Column(db.String(200))
    analysis = db.Column(db.String(500))

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="multiple_choice_questions_bank")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo JudgmentalQuestionBank
class JudgmentalQuestionBank(db.Model):
    __tablename__ = "judgmental_question_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    question = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.Integer, nullable=False)
    knowledge_points = db.Column(db.String(200))
    analysis = db.Column(db.String(500))

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="judgmental_question_bank")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo ShortAnswerQuestionBank
class ShortAnswerQuestionBank(db.Model):
    __tablename__ = "short_answer_question_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    question = db.Column(db.String(500), nullable=False)
    answer = db.Column(db.String(500), nullable=False)
    knowledge_points = db.Column(db.String(200))
    analysis = db.Column(db.String(500))

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="short_answer_question_bank")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo PictureQuestionBank
class PictureQuestionBank(db.Model):
    __tablename__ = "picture_question_bank"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    question = db.Column(db.String(200), nullable=False)
    answer = db.Column(db.String(200), nullable=False)
    knowledge_points = db.Column(db.String(200))
    analysis = db.Column(db.String(500))

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="picture_question_bank")

    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    def init(self):
        self.create_time = get_time()
        self.update_time = get_time()
        self.is_delete = 0


# todo TeacherQuestionBankRel
class TeacherQuestionBankRel(db.Model):
    __tablename__ = "teacher_question_bank_rel"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_question_bank = db.Column(db.Integer, db.ForeignKey("question_bank.id"))
    question_bank = db.relationship("QuestionBank", backref="teacher_question_bank_rel")
    id_teacher = db.Column(db.Integer, db.ForeignKey("teacher.id"))
    teacher = db.relationship("Teacher", backref="teacher_question_bank_rel")


# todo SystemLog
class SystemLog(db.Model):
    __tablename__ = "system_log"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    full_path = db.Column(db.String(200), nullable=False)
    method = db.Column(db.String(10), nullable=False)
    user_agent = db.Column(db.String(200), nullable=False)
    remote_addr = db.Column(db.String(45), nullable=False)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_user = db.Column(db.Integer, db.ForeignKey("user.id"))
    user = db.relationship("User", backref="system_log")


# todo SignIn
class SignIn(db.Model):
    __tablename__ = "sign_in"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.String(45), nullable=False)
    update_time = db.Column(db.String(45), nullable=False)
    is_delete = db.Column(db.Integer, nullable=False)

    # 外键
    id_student = db.Column(db.Integer, db.ForeignKey("student.id"))
    student = db.relationship("Student", backref="sign_in")
    id_classes = db.Column(db.Integer, db.ForeignKey("classes.id"))
    classes = db.relationship("Classes", backref="sign_in")
